Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使媒体播放器同时读取内部和外部存储_Java_Android_Android Sdcard - Fatal编程技术网

Java 如何使媒体播放器同时读取内部和外部存储

Java 如何使媒体播放器同时读取内部和外部存储,java,android,android-sdcard,Java,Android,Android Sdcard,我根据自己观看的教程开发了一款音乐播放器应用程序 当我在没有SD卡的设备上运行时 它崩溃了。所以我不能发布它,所以我可以添加或更改什么 这将使它能够从内部存储器中读取音乐 Java类 package com.example.playaudioexample; import android.app.ListActivity; import android.content.Context; import android.database.Cursor; import android.media.M

我根据自己观看的教程开发了一款音乐播放器应用程序 当我在没有SD卡的设备上运行时 它崩溃了。所以我不能发布它,所以我可以添加或更改什么 这将使它能够从内部存储器中读取音乐

Java类

package com.example.playaudioexample;

import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Handler;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.math.BigDecimal;

public class PlayAudioExample extends ListActivity {

private static final int UPDATE_FREQUENCY = 500;
private static final int STEP_VALUE = 4000;

private MediaCursorAdapter mediaAdapter = null;
private TextView selelctedFile = null;
private SeekBar seekbar = null;
private MediaPlayer player = null;
private ImageButton playButton = null;
private ImageButton prevButton = null;
private ImageButton nextButton = null;
private ImageButton floatButton = null;
private ImageButton floatButton2 = null;

private boolean isStarted = true;
private String currentFile = "";
private boolean isMoveingSeekBar = false;

private final Handler handler = new Handler();

private final Runnable updatePositionRunnable = new Runnable() {
    public void run() {
        updatePosition();
    }
};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.content_play_audio_example);


    selelctedFile = (TextView) findViewById(R.id.selectedfile);
    seekbar = (SeekBar) findViewById(R.id.seekbar);
    playButton = (ImageButton) findViewById(R.id.play);
    prevButton = (ImageButton) findViewById(R.id.prev);
    nextButton = (ImageButton) findViewById(R.id.next);
    floatButton = (ImageButton) findViewById(R.id.imageinfo);
    floatButton2 = (ImageButton)findViewById(R.id.imageMessage);
    floatButton2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(),
                    "CONTACT:gabriel.agbese2001@gmail.com",Toast.LENGTH_LONG).show();
        }
    });
    floatButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(),
                    "Developed by:Gabriel Agbese",Toast.LENGTH_LONG).show();
        }
    });

    player = new MediaPlayer();

    player.setOnCompletionListener(onCompletion);
    player.setOnErrorListener(onError);
    seekbar.setOnSeekBarChangeListener(seekBarChanged);

    Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);

    if (null != cursor) {
        cursor.moveToFirst();

        mediaAdapter = new MediaCursorAdapter(this, R.layout.listitem, cursor);

        setListAdapter(mediaAdapter);

        playButton.setOnClickListener(onButtonClick);
        nextButton.setOnClickListener(onButtonClick);
        prevButton.setOnClickListener(onButtonClick);
    }

}

@Override
protected void onListItemClick(ListView list, View view, int position, long id) {
    super.onListItemClick(list, view, position, id);

    currentFile = (String) view.getTag();

    startPlay(currentFile);
}

@Override
protected void onDestroy() {
    super.onDestroy();

    handler.removeCallbacks(updatePositionRunnable);
    player.stop();
    player.reset();
    player.release();

    player = null;
}

private void startPlay(String file) {
    Log.i("Selected: ", file);

    selelctedFile.setText(file);
    seekbar.setProgress(0);

    player.stop();
    player.reset();

    try {
        player.setDataSource(file);
        player.prepare();
        player.start();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    seekbar.setMax(player.getDuration());
    playButton.setImageResource(android.R.drawable.ic_media_pause);

    updatePosition();

    isStarted = true;
}

private void stopPlay() {
    player.stop();
    player.reset();
    playButton.setImageResource(android.R.drawable.ic_media_play);
    handler.removeCallbacks(updatePositionRunnable);
    seekbar.setProgress(0);

    isStarted = false;
}

private void updatePosition() {
    handler.removeCallbacks(updatePositionRunnable);

    seekbar.setProgress(player.getCurrentPosition());

    handler.postDelayed(updatePositionRunnable, UPDATE_FREQUENCY);
}



private class MediaCursorAdapter extends SimpleCursorAdapter {

    public MediaCursorAdapter(Context context, int layout, Cursor c) {
        super(context, layout, c,
                new String[]{MediaStore.MediaColumns.DISPLAY_NAME, MediaStore.MediaColumns.TITLE, MediaStore.Audio.AudioColumns.DURATION},
                new int[]{R.id.displayname, R.id.title, R.id.duration});
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        TextView title = (TextView) view.findViewById(R.id.title);
        TextView name = (TextView) view.findViewById(R.id.displayname);
        TextView duration = (TextView) view.findViewById(R.id.duration);

        name.setText(cursor.getString(
                cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME)));

        title.setText(cursor.getString(
                cursor.getColumnIndex(MediaStore.MediaColumns.TITLE)));

        long durationInMs = Long.parseLong(cursor.getString(
                cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DURATION)));

        double durationInMin = ((double) durationInMs / 1000.0) / 60.0;

        durationInMin = new BigDecimal(Double.toString(durationInMin)).setScale(2, BigDecimal.ROUND_UP).doubleValue();

        duration.setText("" + durationInMin);

        view.setTag(cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA)));
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.listitem, parent, false);

        bindView(v, context, cursor);

        return v;
    }
}

private View.OnClickListener onButtonClick = new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.play: {
                if (player.isPlaying()) {
                    handler.removeCallbacks(updatePositionRunnable);
                    player.pause();
                    playButton.setImageResource(android.R.drawable.ic_media_play);
                } else {
                    if (isStarted) {
                        player.start();
                        playButton.setImageResource(android.R.drawable.ic_media_pause);

                        updatePosition();
                    } else {
                        startPlay(currentFile);
                    }
                }

                break;
            }
            case R.id.next: {
                int seekto = player.getCurrentPosition() + STEP_VALUE;

                if (seekto > player.getDuration())
                    seekto = player.getDuration();

                player.pause();
                player.seekTo(seekto);
                player.start();

                break;
            }
            case R.id.prev: {
                int seekto = player.getCurrentPosition() - STEP_VALUE;

                if (seekto < 0)
                    seekto = 0;

                player.pause();
                player.seekTo(seekto);
                player.start();

                break;
            }
        }
    }
};

private MediaPlayer.OnCompletionListener onCompletion = new MediaPlayer.OnCompletionListener() {

    @Override
    public void onCompletion(MediaPlayer mp) {
        stopPlay();
    }
};

private MediaPlayer.OnErrorListener onError = new MediaPlayer.OnErrorListener() {

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {

        return false;
    }
};

private SeekBar.OnSeekBarChangeListener seekBarChanged = new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        isMoveingSeekBar = false;
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        isMoveingSeekBar = true;
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        if (isMoveingSeekBar) {
            player.seekTo(progress);

            Log.i("OnSeekBarChangeListener", "onProgressChanged");
        }
    }
};
}
package com.example.playadioexample;
导入android.app.ListActivity;
导入android.content.Context;
导入android.database.Cursor;
导入android.media.MediaPlayer;
导入android.os.Handler;
导入android.provider.MediaStore;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.support.v7.widget.Toolbar;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ImageButton;
导入android.widget.ListView;
导入android.widget.SeekBar;
导入android.widget.SimpleCursorAdapter;
导入android.widget.TextView;
导入android.widget.Toast;
导入java.io.IOException;
导入java.math.BigDecimal;
公共类PlayAudioExample扩展了ListActivity{
私有静态最终整数更新频率=500;
私有静态最终整数步长_值=4000;
私有MediaCursorAdapter mediaAdapter=null;
私有TextView selelctedFile=null;
私有SeekBar SeekBar=null;
私有媒体播放器=null;
私有ImageButton playButton=null;
private ImageButton prevButton=null;
private ImageButton nextButton=null;
私有ImageButton floatButton=null;
私有ImageButton floatButton2=null;
私有布尔值isStarted=true;
私有字符串currentFile=“”;
私有布尔值IsMovingSeekbar=false;
私有最终处理程序=新处理程序();
private final Runnable updatePositionRunnable=new Runnable(){
公开募捐{
updatePosition();
}
};
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.content\u play\u audio\u示例);
selelctedFile=(TextView)findViewById(R.id.selectedfile);
seekbar=(seekbar)findViewById(R.id.seekbar);
playButton=(ImageButton)findViewById(R.id.play);
prevButton=(ImageButton)findViewById(R.id.prev);
nextButton=(ImageButton)findViewById(R.id.next);
floatButton=(ImageButton)findViewById(R.id.imageinfo);
floatButton2=(ImageButton)findViewById(R.id.imageMessage);
floatButton2.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
Toast.makeText(getApplicationContext(),
联系人:加布里埃尔。agbese2001@gmail.com“,Toast.LENGTH_LONG).show();
}
});
floatButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
Toast.makeText(getApplicationContext(),
“开发人:加布里埃尔·阿格贝斯”,Toast.LENGTH_LONG.show();
}
});
player=新媒体播放器();
player.setOnCompletionListener(onCompletion);
player.setOneRorListener(OneRor);
seekbar.setonseekbarchaneglistener(seekBarChanged);
Cursor Cursor=getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null,null,null);
如果(null!=光标){
cursor.moveToFirst();
mediaAdapter=新的MediaCursorAdapter(this,R.layout.listitem,cursor);
setListAdapter(mediaAdapter);
playButton.setOnClickListener(onButtonClick);
setOnClickListener(onButtonClick);
prevButton.setOnClickListener(onButtonClick);
}
}
@凌驾
受保护的void onListItemClick(列表视图列表、视图视图、整数位置、长id){
super.onListItemClick(列表、视图、位置、id);
currentFile=(String)view.getTag();
开始播放(当前文件);
}
@凌驾
受保护的空onDestroy(){
super.ondestory();
removeCallbacks(updatePositionRunnable);
player.stop();
player.reset();
player.release();
player=null;
}
私有void startPlay(字符串文件){
Log.i(“选定:”,文件);
selectedfile.setText(文件);
seekbar.setProgress(0);
player.stop();
player.reset();
试一试{
player.setDataSource(文件);
player.prepare();
player.start();
}捕获(IllegalArgumentException e){
e、 printStackTrace();
}捕获(非法状态){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
seekbar.setMax(player.getDuration());
playButton.setImageResource(android.R.drawable.ic\u media\u pause);
updatePosition();
IsStart=true;
}
私人空间{
player.stop();
player.reset();
playButton.setImageResource(android.R.drawable.ic\u media\u play);
removeCallbacks(updatePositionRunnable);
seekbar.setProgress(0);
isStarted=false;
}
私有void updatePosition(){
removeCallbacks(updatePositionRunnable);
seekbar.setProgress(player.getCurrentPosition());
postDelayed(updatePositionRunnable,更新频率);
}
私有类MediaCursorAdapter扩展了SimpleCursorAdapter{
公共媒体游标适配器(上下文上下文、int布局、游标c){
超级(上下文、布局、c、,
新字符串[]{MediaStore.MediaColumns.DISPLAY_NAME,MediaStore.MediaColumns.TITLE,MediaStore.Audio.AudioColumns.DURATION},
新的int[]{R.id.displayname,R.id.title,R.id.duration});
}
@凌驾
公共void bindView(视图、上下文上下文、光标){
TextView title=(TextView)view.findViewById(R.id.title);
TextView name=(TextView)view.findViewById(R.id.displayname);
TextView duration=(TextView)view.findViewById(R.id.duration);
name.setText(cursor.getString(
cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME));
title.setText(cursor.getString(
光标。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0a0b0e"
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"
tools:context=".PlayAudioExample">

<ListView
    android:id="@android:id/list"
    android:background="#f0fffdfd"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="@android:color/black"
    android:layout_weight="1.0" />
<LinearLayout
    android:layout_width="wrap_content"
    android:background="#f7262624"
    android:layout_height="wrap_content">

<ImageButton
    android:layout_width="65dp"
    android:layout_height="65dp"
    android:background="@drawable/oval"
    android:id="@+id/imageinfo"
    android:src="@android:drawable/ic_dialog_info"
    android:layout_gravity="center_horizontal" />

<ImageButton
    android:layout_width="65dp"
    android:layout_height="65dp"
    android:background="@drawable/oval2"
    android:id="@+id/imageMessage"
    android:src="@android:drawable/ic_dialog_email" />
</LinearLayout>


<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:backgroundTint="#f7262624"
    android:background="@android:drawable/screen_background_light"
    android:orientation="vertical"
    android:padding="10dip">

    <TextView
        android:id="@+id/selectedfile"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ellipsize="middle"
        android:gravity="center_horizontal"
        android:singleLine="true"
        android:text="No file selected"
        android:textColor="@android:color/black" />

    <SeekBar
        android:id="@+id/seekbar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:paddingBottom="10dip" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:drawable/screen_background_light"
        android:gravity="center"
        android:backgroundTint="#ca030b03"
        android:orientation="horizontal">

        <ImageButton
            android:id="@+id/prev"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#000"
            android:src="@android:drawable/ic_media_previous" />

        <ImageButton
            android:id="@+id/play"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/ic_media_play" />

        <ImageButton
            android:id="@+id/next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#000"
            android:src="@android:drawable/ic_media_next" />

    </LinearLayout>

 </LinearLayout>

</LinearLayout>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0a0b0e"
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"
tools:context=".PlayAudioExample">

<ListView
    android:id="@android:id/list"
    android:background="#f0fffdfd"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="@android:color/black"
    android:layout_weight="1.0" />
<LinearLayout
    android:layout_width="wrap_content"
    android:background="#f7262624"
    android:layout_height="wrap_content">

<ImageButton
    android:layout_width="65dp"
    android:layout_height="65dp"
    android:background="@drawable/oval"
    android:id="@+id/imageinfo"
    android:src="@android:drawable/ic_dialog_info"
    android:layout_gravity="center_horizontal" />

<ImageButton
    android:layout_width="65dp"
    android:layout_height="65dp"
    android:background="@drawable/oval2"
    android:id="@+id/imageMessage"
    android:src="@android:drawable/ic_dialog_email" />
</LinearLayout>


<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:backgroundTint="#f7262624"
    android:background="@android:drawable/screen_background_light"
    android:orientation="vertical"
    android:padding="10dip">

    <TextView
        android:id="@+id/selectedfile"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ellipsize="middle"
        android:gravity="center_horizontal"
        android:singleLine="true"
        android:text="No file selected"
        android:textColor="@android:color/black" />

    <SeekBar
        android:id="@+id/seekbar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:paddingBottom="10dip" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:drawable/screen_background_light"
        android:gravity="center"
        android:backgroundTint="#ca030b03"
        android:orientation="horizontal">

        <ImageButton
            android:id="@+id/prev"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#000"
            android:src="@android:drawable/ic_media_previous" />

        <ImageButton
            android:id="@+id/play"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/ic_media_play" />

        <ImageButton
            android:id="@+id/next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#000"
            android:src="@android:drawable/ic_media_next" />

    </LinearLayout>

 </LinearLayout>

</LinearLayout>