安卓音乐播放器-为何音乐播放器代码在esclipse中没报错,点击按钮却没有实现功能,上图上代码,求前辈们指教

问题描述

为何音乐播放器代码在esclipse中没报错,点击按钮却没有实现功能,上图上代码,求前辈们指教


Mainactivity
package com.example.music;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore.Audio.Media;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity implements OnClickListener {
List musiclists = new ArrayList();
List> list = new ArrayList>();
Button play_pause, stop, onplay, downplay, close, exit;
ActivityReceiver activityReceiver;
public static final String CTL_ACTION = "org.crazyit.action.CTL_ACTION";
public static final String UPDATE_ACTION = "org.crazyit.action.UPDATE_ACTION";
Intent intentservice;
// 定义音乐的播放状态 ,0X11 代表停止 ,0x12代表播放,0x13代表暂停
int status = 0x11;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    UIinit();
    logic();
    musicList();
    activityReceiver = new ActivityReceiver();
    IntentFilter filter = new IntentFilter();
    filter.addAction(UPDATE_ACTION);
    registerReceiver(activityReceiver, filter);
    intentservice = new Intent(this, MusicService.class);
    startService(intentservice);
}

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

public void UIinit() {
    play_pause = (Button) this.findViewById(R.id.play_pause);
    stop = (Button) this.findViewById(R.id.stop);
    onplay = (Button) this.findViewById(R.id.onplay);
    close = (Button) this.findViewById(R.id.close);
    exit = (Button) this.findViewById(R.id.exit);
    downplay = (Button) this.findViewById(R.id.downplay);
}

public void logic() {
    play_pause.setOnClickListener(this);
    stop.setOnClickListener(this);
    onplay.setOnClickListener(this);
    downplay.setOnClickListener(this);
    close.setOnClickListener(this);
    exit.setOnClickListener(this);
}

@Override
public void onClick(View source) {
    Intent intent = new Intent(CTL_ACTION);
    switch (source.getId()) {
    case R.id.play_pause: {
        intent.putExtra("control", 1);
        break;
    }
    case R.id.stop: {
        intent.putExtra("control", 2);
        break;
    }
    case R.id.onplay: {
        intent.putExtra("control", 3);
        break;
    }
    case R.id.downplay: {
        intent.putExtra("control", 4);
        break;
    }
    case R.id.close: {
        this.finish();
        break;
    }
    case R.id.exit: {
        stopService(intentservice);
        this.finish();
        break;
    }
    }
    sendBroadcast(intent);

}

public class ActivityReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // 获取Intent中的update消息,update代表播放状态
        int update = intent.getIntExtra("update", -1);
        switch (update) {
        case 0x11: {
            play_pause.setText("播放");
            status = 0x11;
            break;
        }

        // 控制系统进入播放状态
        case 0x12: {
            // 播放状态下设置使用按钮
            play_pause.setText("暂停");
            // 设置当前状态
            status = 0x12;
            break;
        }
        // 控制系统进入暂停状态
        case 0x13: {
            play_pause.setText("播放");
            status = 0x13;
            break;
        }
        }
    }

}

/* 播放列表 */
public void musicList() {
    // 取得指定位置的文件设置显示到播放列表
    String[] music = new String[] { Media._ID, Media.DISPLAY_NAME,
            Media.TITLE, Media.DURATION, Media.ARTIST, Media.DATA };
    Cursor cursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI,
            music, null, null, null);
    while (cursor.moveToNext()) {
        Music temp = new Music();
        temp.setFilename(cursor.getString(1));
        temp.setTitle(cursor.getString(2));
        temp.setDuration(cursor.getInt(3));
        temp.setArtist(cursor.getString(4));
        temp.setData(cursor.getString(5));
        musiclists.add(temp);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", cursor.getString(1));
        map.put("artist", cursor.getString(4));
        list.add(map);
    }

    ListView listview = (ListView) findViewById(R.id.musics);
    SimpleAdapter adapter = new SimpleAdapter(this, list,
            R.layout.musicsshow, new String[] { "name", "artist" },
            new int[] { R.id.name, R.id.artist });
    listview.setAdapter(adapter);
    listview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int current, long id) {

            Intent intent=new Intent(CTL_ACTION);
            intent.putExtra("control", 5);
            intent.putExtra("current", current);
            sendBroadcast(intent);
        }
    });
}

}

TitanMusic.java
package com.example.titan;

public class TitanMusic {

private String filename;
private String Title;
private int   duration;
private String artist;
private String location;

public String getFilename() {
    return filename;
}
public void setFilename(String filename) {
    this.filename = filename;
}
public String getTitle() {
    return Title;
}
public void setTitle(String title) {
    Title = title;
}
public int getDuration() {
    return duration;
}
public void setDuration(int duration) {
    this.duration = duration;
}
public String getArtist() {
    return artist;
}
public void setArtist(String artist) {
    this.artist = artist;
}
public String getData() {
    return location;
}
public void setData(String location) {
    this.location = location;
}

// {MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DISPLAY_NAME,
// MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DURATION,
// MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.DATA
}
TitanMusicService.java
package com.example.titan;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.IBinder;
import android.provider.MediaStore.Audio.Media;
import android.util.Log;

public class TitanMusicService extends Service {
List musiclists = new ArrayList();
List> list = new ArrayList>();
MyReceiver serviceReceiver;
MediaPlayer mPlayer;
int status = 0x11;
//歌曲下标
int current = 0;
int count = 0;
int flog = 0;
//String filename;
@Override
public IBinder onBind(Intent arg0) {

    return null;
}

@Override
public void onDestroy() {
    flog=0;
    mPlayer.stop();
    mPlayer.release();
    super.onDestroy();
}

@Override
public void onCreate() {
    flog=1;
    musicList();
    count = list.size();
    Log.e("Service", "onStart");
    serviceReceiver = new MyReceiver();
    IntentFilter filter = new IntentFilter();
    filter.addAction(MainActivity.CTL_ACTION);
    registerReceiver(serviceReceiver, filter);
    mPlayer = new MediaPlayer();
    mPlayer.setOnCompletionListener(new OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            current++;
            if (current >= count) {
                current = 0;
            }
            String filename = ((TitanMusic) musiclists.get(current)).getData();
            playMusic(filename);
        }
    });

    super.onCreate();
}

@Override
public void onStart(Intent intent, int startId) {
    if(flog==2){
        Intent sendIntent = new Intent(MainActivity.UPDATE_ACTION);
        sendIntent.putExtra("update", status);
        //sendIntent.putExtra("current", current);
        sendBroadcast(sendIntent);
    }
    flog=2;
}

 private void playMusic(String path)
    {
        try
        {
            /* 重置MediaPlayer */
            mPlayer.reset();
            /* 设置要播放的文件的路径 */
            mPlayer.setDataSource(path);
            /* 准备播放 */
            mPlayer.prepare();
            /* 开始播放 */
            mPlayer.start();
            mPlayer.setOnCompletionListener(new OnCompletionListener()
            {
                public void onCompletion(MediaPlayer arg0)
                {
                    //播放完成一首之后进行下一首
                    current--;
                    if (current < 0) {
                        current = count;
                    }
                    playMusic( ((TitanMusic) musiclists.get(current)).getData());
                    status = 0x12;
                }
            });
        }catch (IOException e){}
    }   

    /* 播放列表 */
    public void musicList() {
        // 取得指定位置的文件设置显示到播放列表
        String[] music = new String[] { Media._ID, Media.DISPLAY_NAME,
                Media.TITLE, Media.DURATION, Media.ARTIST, Media.DATA };
        Cursor cursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI,
                music, null, null, null);
        while (cursor.moveToNext()) {
            TitanMusic temp = new TitanMusic();
            temp.setFilename(cursor.getString(1));
            temp.setTitle(cursor.getString(2));
            temp.setDuration(cursor.getInt(3));
            temp.setArtist(cursor.getString(4));
            temp.setData(cursor.getString(5));
            musiclists.add(temp);

            Map<String, Object> map = new HashMap<String, Object>();
            map.put("name", cursor.getString(1));
            map.put("artist", cursor.getString(4));
            list.add(map);
        }
    }

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        int control = intent.getIntExtra("control", -1);
        switch (control) {
        case 1: {
            // 如果原来处于停止的状态
            if (status == 0x11) {
                playMusic( ((TitanMusic) musiclists.get(current)).getData());
                status = 0x12;
            }
            // 原来处于播放状态
            else if (status == 0x12) {
                mPlayer.pause();
                status = 0x13;
            }
            // 原来处于暂停状态
            else if (status == 0x13) {
                mPlayer.start();
                status = 0x12;
            }
            break;
        }
        case 2: {
            // 如果原来正在播放或暂停
            if (status == 0x12 || status == 0x13) {
                mPlayer.stop();
                status = 0x11;
            }
            break;
        }
        case 3: {
            current--;
            if (current < 0) {
                current = count;
            }
            playMusic( ((TitanMusic) musiclists.get(current)).getData());
            status = 0x12;
            break;
        }
        case 4: {
            current++;
            if (current > count) {
                current = 0;
            }
            playMusic( ((TitanMusic) musiclists.get(current)).getData());
            status = 0x12;
            break;
        }
        case 5: {
            current = intent.getIntExtra("current", -1);;
            playMusic( ((TitanMusic) musiclists.get(current)).getData());
            status = 0x12;
            break;
        }
        }
        Intent sendIntent = new Intent(MainActivity.UPDATE_ACTION);
        sendIntent.putExtra("update", status);
    //  sendIntent.putExtra("current", current);
        sendBroadcast(sendIntent);
    }

}

}
布局代码:
<?xml version="1.0" encoding="utf-8"?>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/onplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="上一首" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/play_pause"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="播放" />

        <Button
            android:id="@+id/stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="停止" />

    </LinearLayout>
    <RelativeLayout
         android:layout_width="match_parent"
    android:layout_height="wrap_content">

       <Button
           android:id="@+id/downplay"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignParentRight="true"
           android:layout_alignParentTop="true"
           android:text="下一首" />

     </RelativeLayout>
</LinearLayout>
<!--
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="音乐列表"
    android:textColor="#ff0000ff"
    android:textSize="30sp" />
-->
<ListView
    android:id="@+id/musics"
    android:layout_width="match_parent"
    android:layout_height="257dp"
    android:layout_weight="1.53" >
</ListView>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
  <RelativeLayout
       android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Button
        android:id="@+id/close"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="最小化" />

    <Button
        android:id="@+id/exit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="退出" />
</RelativeLayout>
</LinearLayout>

歌曲布局:
<?xml version="1.0" encoding="utf-8"?>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="20dp" />

<TextView
    android:id="@+id/artist"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="20dp" />

解决方案

解决方案二:

求前辈们看一下啊啊啊啊啊啊啊啊啊,这是我们的作业啊

解决方案三:

报啥错了?弄个logcat 的图出来,可能是按钮事件出错了,你在仔细检查下按钮事件

时间: 2024-08-03 13:05:31

安卓音乐播放器-为何音乐播放器代码在esclipse中没报错,点击按钮却没有实现功能,上图上代码,求前辈们指教的相关文章

安卓简单app开发实践,代码完成后没报错,但功能就是无法使用,学生菜鸟,求指导

问题描述 安卓简单app开发实践,代码完成后没报错,但功能就是无法使用,学生菜鸟,求指导 想把网上一个简单记事本的功能(编辑,保存,修改,删除)添加入一个含三个Fragment的app框架中,但添加并把提示的错误修改后发现功能根本不管用,图标都不能点击,运行结果中console显示[2015-07-14 22:44:42 - ddms] Can't bind to local 8700 for debugger. 解决方案 看下防火墙和杀毒软件设置,提示是端口没法绑定. 解决方案二: 直接使用真

android-Android开发问题,安卓环境搭建完成后,调试过程中eclipse报错的问题

问题描述 Android开发问题,安卓环境搭建完成后,调试过程中eclipse报错的问题 安卓环境搭建 eclipse报 The connection to adb is down and a severe error has occured.You must restart adb and Eclipse.Please ensure that adb is correctly located at 'D:shithashadt-bundle-windows-x86_64-20140321sdk

svn-我win10 系统 新装的安卓studio 从SVN导下来的项目 打开 后一运行就报错

问题描述 我win10 系统 新装的安卓studio 从SVN导下来的项目 打开 后一运行就报错

java-浏览器通过URL访问Controller,是可以下载的,项目中点击按钮不能下载?

问题描述 浏览器通过URL访问Controller,是可以下载的,项目中点击按钮不能下载? 项目前端:Angular JS,Required JS,nginx代理服务器:后台Java,Spring MVC. 现在有一个下载问题:直接通过URL访问Controller,是可以下载的,并且弹出下载框,从项目里面点击按钮下载不弹出下载框,请问这是什么原因?一般不是只要设置了Content-Type,Header,就可以了吗? 解决方案 用fiddler看下,返回了什么错误,还是干脆没有触发下载. 解决

编程-在真机上运行做好的安卓程序时,点击按钮跳转界面就停止运行怎么解决

问题描述 在真机上运行做好的安卓程序时,点击按钮跳转界面就停止运行怎么解决 求各路神仙帮忙 小弟一名安卓编程新手中的新手 做好了安卓程序 代码没有错误 但是在真机上运行时 第一个界面可以出来 但当点击按钮进入下一个界面时程序 就停止运行了 并且Logcat也报错 怎么解决 请求帮忙 谢谢了 解决方案 那你就看Log报的是什么错啊.跳转的目的控件要去清单文件中配置,看看是不是忘了配置了 解决方案二: 看看logcat报什么错 这边情况一般是点击事件那里 或者 你要跳转的Activity 有错误

网易云音乐如何设置为默认播放器

  网易云音乐怎么设置为默认播放器?网易云音乐,是一个简单实用而又功能强大的音乐播放器,深受广大群众的喜爱.音乐无界限,听见好时光.平时电脑使用网易云音乐播放器来播放音乐,那么就要将它设置为默认音乐播放器了.下面,安下小编给大家带来网易云音乐设置为默认播放器的方法. 网易云音乐设置为默认播放器的两种方法 方法一: 1.鼠标左键双击桌面上的网易云播放器,打开它. 2.打开之后,来到播放器的首页,我们点击右上角的设置按钮. 3.然后在这我们就可以将它设置为默认播放器了. 方法二: 1.点击桌面左下角

as3 音乐播放器带控制播放进度

点击进度条音乐从鼠标点击处开始播放 制作过程: 1.新建FLASH CS3文档,保存名为"控制播放进度",保存在存有音乐的文件夹内.设置文档大小:252×114像素,帧频:24fps. 2.第一层名为"播放器背景",在该层画一个播放器背景. 3.新建图层,名为"播放进度框",画一个无边框的圆角矩形,边角半径为8,宽202高6,填充颜色为#006600,使其垂直水平于舞台. 4.新建图层,名为"播放进度背景",画一个无边框的圆角

mp3-java编的音乐播放器,只能播放部分MP3文件

问题描述 java编的音乐播放器,只能播放部分MP3文件 import java.io.File; import java.io.IOException; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.DataLine; import jav

22_Android中的本地音乐播放器和网络音乐播放器的编写,本地视频播放器和网络视频播放器,照相机案例,偷拍案例实现

1 编写以下案例: 当点击了"播放"之后,在手机上的/mnt/sdcard2/natural.mp3就会播放. 2 编写布局文件activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layo