Android Media Player(设置dataSource失败)

时间:2016-07-25 02:27:19

标签: android xml android-mediaplayer listactivity

嗨,所以我从一个基本的音乐应用程序开始,不要因为我没有使用recyclerview而讨厌我。但该应用程序运行得很好,没有力量关闭,但是当我点击一首歌时歌曲不会播放。 android moniter说“ 音乐应用测试1:setDataSource失败“ 我不确定我的问题是否与我的目录有关,所以请告诉我。 如果您需要查看xml文件,请告诉我。

    package com.example.abhishek.musicapptest1;

import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.view.View.OnClickListener;
import android.view.View.OnHoverListener;

import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

class Mp3filter implements FilenameFilter {// filters all files that are mp3

    @Override
    public boolean accept(File directory, String song_name) { // will return mp3
        return (song_name.endsWith(".mp3"));
    }
}

public class MainActivity extends ListActivity {

    private static final String SD_PATH = new String("/sdcard/Music/Phone Music");

    private List<String> songs = new ArrayList<String>();
    private MediaPlayer mp = new MediaPlayer();
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        //setSupportActionBar(toolbar);

        updatePlaylist();

        Button stopPlay = (Button) findViewById(R.id.stopBtn);
        stopPlay.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mp.stop();
            }

        });


        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

    protected void onListItemClick(ListView list, View view, int position, long id) {
        try {
            mp.reset();
            mp.setDataSource(SD_PATH + songs.get(position));
            mp.prepare();
            mp.start();
        } catch (IOException e) {
            Log.v(getString(R.string.app_name),e.getMessage());
        }
    }

    private void updatePlaylist() {// will update the play list, will find the sd card
        // we need to filter out the files which we dont want

        File home = new File(SD_PATH);
        if (home.listFiles(new Mp3filter()).length > 0) {
            for (File file : home.listFiles(new Mp3filter())) {
                songs.add(file.getName());
            }

            ArrayAdapter<String> songList = new ArrayAdapter<String>(this, R.layout.song_view, songs);
            setListAdapter(songList);

        }

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    @Override
    public void onStart() {
        super.onStart();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client.connect();
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "Main Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                // make sure this auto-generated web page URL is correct.
                // Otherwise, set the URL to null.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app URL is correct.
                Uri.parse("android-app://com.example.abhishek.musicapptest1/http/host/path")
        );
        AppIndex.AppIndexApi.start(client, viewAction);
    }

    @Override
    public void onStop() {
        super.onStop();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "Main Page", // TODO: Define a title for the content shown.
                // TODO: If you have web page content that matches this app activity's content,
                // make sure this auto-generated web page URL is correct.
                // Otherwise, set the URL to null.
                Uri.parse("http://host/path"),
                // TODO: Make sure this auto-generated app URL is correct.
                Uri.parse("android-app://com.example.abhishek.musicapptest1/http/host/path")
        );
        AppIndex.AppIndexApi.end(client, viewAction);
        client.disconnect();
    }
}

1 个答案:

答案 0 :(得分:1)

从您的代码中,请检查以下行:

private static final String SD_PATH = new String("/sdcard/Music/Phone Music");
songs.add(file.getName());
mp.setDataSource(SD_PATH + songs.get(position));

因此,您的DataSource不是有效的文件名,缺少反斜杠。