huangcm
2025-04-09 02d4ce54b909bd733f12e9f3fa4c1b03cf2d6f45
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package com.DeviceTest;
 
import static android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN;
import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
 
import java.io.FileDescriptor;
import java.io.IOException;
 
import com.DeviceTest.helper.ControlButtonUtil;
 
import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Handler;
import android.os.Vibrator;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
 
public class SpeakerTestActivity extends Activity {
   private AudioManager mAudioManager;
   private MediaPlayer mPlayer;
   private int mOldVolume;
   private boolean mSpeakerOn;
 
   private Button leftButton;
   private Button rightButton;
 
   private boolean leftEnable = true;
   private boolean rightEnable = true;
 
   
   protected void onCreate(Bundle paramBundle) {
       super.onCreate(paramBundle);
       
 
       setTitle(getTitle() + "----("
               + getIntent().getStringExtra(DeviceTest.EXTRA_TEST_PROGRESS) + ")");
       //requestWindowFeature(Window.FEATURE_NO_TITLE);
       getWindow().addFlags(FLAG_FULLSCREEN | FLAG_KEEP_SCREEN_ON);
       
       setContentView(R.layout.speakertest);
       TextView txtTitle = (TextView) findViewById(R.id.txtTitle);
       TextView txtContent = (TextView) findViewById(R.id.txtContent);
       txtTitle.setText(R.string.SpeakerTitle);
       txtContent.setText(getString(R.string.SpeakerTip));
 
       mAudioManager = (AudioManager) getSystemService("audio");
       mPlayer = new MediaPlayer();
       try {
           // mPlayer.setDataSource("/system/media/audio/ringtones/CrazyDream.ogg");
           AssetFileDescriptor fd = getAssets().openFd("test_music.mp3");
           mPlayer.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(),
                   fd.getDeclaredLength());
 
           mPlayer.prepare();
           mPlayer.setLooping(true);
       } catch (IllegalArgumentException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       } catch (IllegalStateException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
       leftButton = (Button) findViewById(R.id.spk_btn_left);
       leftButton.setOnClickListener(new OnClickListener() {
 
           
           public void onClick(View v) {
               leftEnable = !leftEnable;
               updateButtons();
           }
       });
 
       rightButton = (Button) findViewById(R.id.spk_btn_right);
       rightButton.setOnClickListener(new OnClickListener() {
 
           
           public void onClick(View v) {
               rightEnable = !rightEnable;
               updateButtons();
           }
       });
       updateButtons();
 
       ControlButtonUtil.initControlButtonView(this);
       
   }
 
   protected void updateButtons() {
       leftButton.setText("left " + (leftEnable ? "enabled" : "disabled"));
       rightButton.setText("right " + (rightEnable ? "enabled" : "disabled"));
 
       mPlayer.setVolume(leftEnable ? 1 : 0, rightEnable ? 1 : 0);
   }
 
   protected void onDestroy() {
       super.onDestroy();
       mPlayer.stop();
       if (this.mPlayer == null) {
           return;
       }
       this.mPlayer.release();
       this.mPlayer = null;
   }
 
   protected void onPause() {
       super.onPause();
       this.mAudioManager.setStreamSolo(AudioManager.STREAM_MUSIC, false);
       this.mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
               this.mOldVolume, 0);
       if (this.mSpeakerOn)
           return;
       this.mAudioManager.setSpeakerphoneOn(false);
 
   }
 
   protected void onResume() {
       super.onResume();
       stopMediaPlayBack();
       this.mAudioManager.setStreamSolo(AudioManager.STREAM_MUSIC, true);
       int i = this.mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
       mOldVolume = i;
       int j = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
       mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, j, 0);
       this.mSpeakerOn = this.mAudioManager.isSpeakerphoneOn();
       if (!this.mSpeakerOn) {
           this.mAudioManager.setSpeakerphoneOn(true);
       }
       this.mPlayer.start();
 
   }
 
   private void stopMediaPlayBack() {
       Intent i = new Intent("com.android.music.musicservicecommand");
       i.putExtra("command", "pause");
       sendBroadcast(i);
 
   }
 
   public boolean dispatchKeyEvent(KeyEvent event) {
       if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
           return false;
       }
       return super.dispatchKeyEvent(event);
   }
}