【问题标题】:When I run mediaplayer app on android virtual device error comes saying that unfortunately my app has stopped.当我在 android 虚拟设备上运行 mediaplayer 应用程序时出现错误,说不幸的是我的应用程序已停止。
【发布时间】:2016-08-26 13:22:53
【问题描述】:

当我在 android 虚拟设备上运行 mediaplayer 应用程序时出现错误提示 mediaplayer 已停止?代码中的错误是什么?

当我在 android 虚拟设备上运行 mediaplayer 应用程序时出现错误提示 mediaplayer 已停止?

****这是android音乐播放器的清单****

 <?xml version="1.0" encoding="utf-8"?>

  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidmediaplayer"
android:versionCode="1"
android:versionName="1.0" >

  <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

  <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name="com.example.AndroidMediaPlayer"
        android:screenOrientation="portrait"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
  </application>

   </manifest>

  **This is the java code:**

  package com.example.androidmediaplayer;

     import java.util.concurrent.TimeUnit;

     import com.example.androidmediaplayer.R;

      import android.app.Activity;
       import android.media.MediaPlayer;
       import android.os.Bundle;
       import android.os.Handler;
      import android.view.View;
      import android.widget.SeekBar;
      import android.widget.TextView;

  public class  MainActivity extends Activity {

private MediaPlayer mediaPlayer;
public TextView songName, duration;
private double timeElapsed = 0, finalTime = 0;
private int forwardTime = 2000, backwardTime = 2000;
private Handler durationHandler = new Handler();
private SeekBar seekbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //set the layout of the Activity
    setContentView(R.layout.activity_main);

    //initialize views
    initializeViews();
}

public void initializeViews(){
    songName=(TextView)findViewById(R.id.SongName);


    seekbar.setMax((int) finalTime);
    seekbar.setClickable(false);
}

// play mp3 song
public void play(View view) {
    mediaPlayer.start();
    timeElapsed = mediaPlayer.getCurrentPosition();
    seekbar.setProgress((int) timeElapsed);
    durationHandler.postDelayed(updateSeekBarTime, 100);
}

//handler to change seekBarTime
private Runnable updateSeekBarTime = new Runnable() {

    public void run() {
        //get current position
        timeElapsed = mediaPlayer.getCurrentPosition();
        //set seekBar progress
        seekbar.setProgress((int) timeElapsed);
        //set time remaining
        double timeRemaining = finalTime - timeElapsed;
        duration.setText(String.format("%d min, %d sec",                 TimeUnit.MILLISECONDS.toMinutes((long) timeRemaining), TimeUnit.MILLISECONDS.toSeconds((long) timeRemaining) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) timeRemaining))));

        //repeat yourself that again in 100 milliseconds
        durationHandler.postDelayed(this, 100);
    }
    };

   // pause mp3 song
   public void pause(View view) {
    mediaPlayer.pause();
   }

    // go forward at forwardTime seconds
   public void forward(View view) {
    //check if we can go forward at forwardTime seconds before song 
    if ((timeElapsed + forwardTime) <= finalTime) {
        timeElapsed = timeElapsed + forwardTime;

        //seek to the exact second of the track
        mediaPlayer.seekTo((int) timeElapsed);
    }
   }

   // go backwards at backwardTime seconds
       public void rewind(View view) {
    //check if we can go back at backwardTime seconds after song starts
    if ((timeElapsed - backwardTime) > 0) {
        timeElapsed = timeElapsed - backwardTime;

        //seek to the exact second of the track
        mediaPlayer.seekTo((int) timeElapsed);
           }
        }

      } 

 **This is XML code:**

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#333333"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >

<TextView
    android:id="@+id/SongName"
    android:layout_width="wrap_content"
    android:textIsSelectable="true"
    android:text="@string/songName"
    android:layout_height="wrap_content"/>


<ImageView
    android:id="@+id/s_mp3Image"
    android:layout_width="match_parent"
    android:layout_height="200dp"
     android:padding="30dp"
     android:contentDescription="@string/music"
    android:src="@drawable/music"
    android:background="#ffffff"
    android:layout_margin="30dp" />

<TextView
   android:id="@+id/songDuration"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textIsSelectable="true"
    android:text="@string/songDuration"
    android:layout_gravity="center"/>

<SeekBar
    android:id="@+id/s_seekBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="30dp"
    android:gravity="center_horizontal"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/s_media_rew"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="14dp"
        android:onClick="rewind"
        android:contentDescription="@string/ic_media_rew"
        android:src="@android:drawable/ic_media_rew" />

    <ImageButton
        android:id="@+id/s_media_pause"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="14dp"
        android:onClick="pause"
        android:contentDescription="@string/media_pause"
        android:src="@android:drawable/ic_media_pause" />

    <ImageButton
        android:id="@+id/s_media_play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="14dp"
        android:onClick="play"
        android:contentDescription="@string/ic_media_play"
        android:src="@android:drawable/ic_media_play" />

    <ImageButton
        android:id="@+id/s_media_ff"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="14dp"
        android:onClick="forward"
        android:contentDescription="@string/ic_media_ff"
        android:src="@android:drawable/ic_media_ff" />
</LinearLayout>

【问题讨论】:

    标签: android eclipse compilation android-emulator virtual-machine


    【解决方案1】:

    尝试调用

    mediaPlayer = new MediaPlayer();
    mediaPlayer.setDataSource(/*uri to your data source*/);
    mediaPlayer.prepare();
    

    之前:

    mediaPlayer.start();
    

    您也可以查看this 资源以获得更好的播放器库

    【讨论】:

    • 我在 mediaPlayer.start() 之前添加了这两行,mediaPlayer.prepare() 中出现错误;即“未处理的异常类型 IOException”
    • 在设置数据源中我应该写什么?
    • 要播放的 mp3 文件的 uri。例如:Uri.fromFile(new File("/sdcard/sample.mp3"))
    猜你喜欢
    • 2018-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多