【发布时间】:2016-07-27 15:00:33
【问题描述】:
基本上,我的主要活动是通过一个意图调用另一个具有应该播放声音的按钮的活动。
也就是说,在第二个活动中我有这个方法:
public void start_sound(View v) {
String url = /*"MY_URL"*/;
try {
if (mp != null)
{
mp.stop();
mp.release();
mp = null;
}
mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setDataSource(url);
mp.prepare();
mp.start();
} catch (Exception e) {}
}
mp 是一个全局变量(所以我可以检查何时正在播放或为空)。
这是我点击按钮时发生的情况:
E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb40d7400
W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=676.58203, y[0]=318.10547, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=866058, downTime=860523, deviceId=0, source=0x1002 }
W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=676.58203, y[0]=318.10547, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=866058, downTime=860523, deviceId=0, source=0x1002 }
W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=676.58203, y[0]=318.10547, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=866058, downTime=860523, deviceId=0, source=0x1002 }
W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=676.58203, y[0]=318.10547, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=866058, downTime=860523, deviceId=0, source=0x1002 }
E/MediaPlayer: error (1, -2147483648)
I/Choreographer: Skipped 1806 frames! The application may be doing too much work on its main thread.
第一个发生在我根据意图更改活动时;其他的,当我按下声音按钮时。
我已经拥有清单中的权限。
编辑:这是完整的课程:
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class activity_2 extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_2);
}
public void start_sound(View v) {
String url = "<MY_URL>";
MediaPlayer mp = new MediaPlayer();
try {
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setDataSource(url);
mp.setOnPreparedListener(this);
mp.prepare();
mp.start();
} catch (Exception ignored) {
}
}
}
【问题讨论】:
标签: android button android-intent audio-player