1. Are you ready for the Galaxy S20? Here is everything we know so far!

Help to fix the Audio Music Player Errors!!

Discussion in 'Android Development' started by Quencyjones79, Feb 28, 2021.

  1. Quencyjones79

    Quencyjones79 Lurker
    Thread Starter

    Hello good morning for everyone I´m a beginner on the Android Studio, and i need a help for you to place well the app, that means working perfectly. It´s a good app, and it could serve me as a example to understand its funcionality.

    I´m building an audio music player app., but on my MainActivity.java file ,


    In the code i faced with some mistakes, see below the images that i sent you. How could you fix them?


    MainActivity.java


    Code (Java):
    1. package com.example.musicaudioplayer;
    2.  
    3.  
    4. import androidx.annotation.NonNull;
    5.  
    6. import androidx.annotation.Nullable;
    7.  
    8. import androidx.appcompat.app.AppCompatActivity;
    9.  
    10. import androidx.core.app.ActivityCompat;
    11.  
    12. import androidx.core.content.ContextCompat;
    13.  
    14. import androidx.fragment.app.Fragment;
    15.  
    16. import androidx.fragment.app.FragmentManager;
    17.  
    18. import androidx.fragment.app.FragmentPagerAdapter;
    19.  
    20. import androidx.viewpager.widget.ViewPager;
    21.  
    22.  
    23. import android.content.Context;
    24.  
    25. import android.content.pm.PackageManager;
    26.  
    27. import android.net.Uri;
    28.  
    29. import android.os.Bundle;
    30.  
    31. import android.provider.MediaStore;
    32.  
    33. import android.widget.Toast;
    34.  
    35.  
    36. import com.google.android.material.tabs.TabLayout;
    37.  
    38.  
    39. import java.util.ArrayList;
    40.  
    41. import java.util.jar.Manifest;
    42.  
    43.  
    44. public class MainActivity extends AppCompatActivity {
    45.  
    46.     public static final int REQUEST_CODE=1;
    47.  
    48.     ArrayList<MusicFiles>musicFiles;
    49.  
    50.     [USER=1021285]@override[/USER]
    51.  
    52.     protected void onCreate(Bundle savedInstanceState) {
    53.  
    54.         super.onCreate(savedInstanceState);
    55.  
    56.         setContentView(R.layout.activity_main);
    57.  
    58.         permission();
    59.  
    60.         initViewpager();
    61.  
    62.     }
    63.  
    64.  
    65.     private void permission() {
    66.  
    67.         if(ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE)
    68.  
    69.         != PackageManager.PERMISSION_GRANTED) {
    70.  
    71.             ActivityCompat.requestPermissions(MainActivity.this, new String[]{
    72.  
    73.                             Manifest.permission.WRITE_EXTERNAL_STORAGE}
    74.  
    75.                     , REQUEST_CODE);
    76.  
    77.         }
    78.  
    79.                 else
    80.  
    81.         {
    82.  
    83.             Toast.makeText(this, "Permission Granted!", Toast.LENGTH_SHORT).show();
    84.  
    85.             musicFiles = getAllAudio(this);
    86.  
    87.         }
    88.  
    89.     }
    90.  
    91.     [USER=1021285]@override[/USER]
    92.  
    93.         public void onRequestPermissionsResult (int requestCode,@NonNull String[]permissions,
    94.  
    95.                                                 @NonNull int[] grantResults){
    96.  
    97.         super.onRequestPermissionsResult(requestCode,permissions,grantResults);
    98.  
    99.         if (requestCode==REQUEST_CODE)
    100.  
    101.         {
    102.  
    103.             if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
    104.  
    105.                 {
    106.  
    107.                   //Do whatever you want permission related
    108.  
    109.                     Toast.makeText(this, "Permission Granted !", Toast.LENGTH_SHORT).show();
    110.  
    111.                     musicFiles =getAllAudio(this);
    112.  
    113.  
    114.                 }
    115.  
    116.                 else
    117.  
    118.                 {
    119.  
    120.                     ActivityCompat.requestPermissions(MainActivity.this, new String []{Manifest.permission.WRITE_EXTERNAL_STORAGE,
    121.  
    122.                             REQUEST_CODE);
    123.  
    124.                     }
    125.  
    126.                     }
    127.  
    128.             }
    129.  
    130.  
    131.     private void initViewpager(){
    132.  
    133.         ViewPager viewPager =findViewById(R.id.viewpager);
    134.  
    135.         TabLayout tabLayout = findViewById(R.id.tab_layout);
    136.  
    137.         ViewpagerAdapter viewPagerAdapter = new ViewpagerAdapter(getSupportFragmentManager());
    138.  
    139.         viewPagerAdapter.addFragments(new SongsFragment(), "Songs");
    140.  
    141.         viewPagerAdapter.addFragments(new AlbumFragment(),"Album");
    142.  
    143.         viewPager.setAdapter(viewPagerAdapter);
    144.  
    145.         tabLayout.setupWithViewPager(viewPager);
    146.  
    147.     }
    148.  
    149.     public static class ViewpagerAdapter extends FragmentPagerAdapter{
    150.  
    151.  
    152.         private ArrayList<Fragment> fragments;
    153.  
    154.         private ArrayList<String> titles;
    155.  
    156.         public ViewpagerAdapter (@NonNull FragmentManager fm){
    157.  
    158.             super(fm);
    159.  
    160.             this.fragments = new ArrayList<>();
    161.  
    162.             this.titles = new ArrayList<>();
    163.  
    164.         }
    165.  
    166.         void addFragments (Fragment fragment, String title){
    167.  
    168.             fragments.add(fragment);
    169.  
    170.             titles.add(title);
    171.  
    172.  
    173.         }
    174.  
    175.  
    176.         @NonNull
    177.  
    178.         [USER=1021285]@override[/USER]
    179.  
    180.         public Fragment getItem (int position){
    181.  
    182.             return fragments.get(position);
    183.  
    184.         }
    185.  
    186.         [USER=1021285]@override[/USER]
    187.  
    188.         public int getCount(){
    189.  
    190.             return fragments.size();
    191.  
    192.         }
    193.  
    194.  
    195.         [USER=1996173]@nullable[/USER]
    196.  
    197.         [USER=1021285]@override[/USER]
    198.  
    199.         public CharSequence getPageTitle(int position){
    200.  
    201.             return titles.get(position);
    202.  
    203.         }
    204.  
    205.     }
    206.  
    207. }
    208.  
    209.     }
    210.  
    211. }
    212.  
    213. public static ArrayList <cMusicFiles> getAllAudio(Context context){
    214.  
    215.   ArrayList<MusicFiles> tempAudioList = new ArrayList<>();
    216.  
    217.     Uri uri= MediaStore.Audio.EXTERNAL_CONTENT_URI;
    218.  
    219.     String[] projection = (
    220.  
    221.             MediaStore.Audio.Media.ALBUM,
    222.  
    223.             MediaStore.Audio.Media.TITLE,
    224.  
    225.             MediaStore.Audio.Media.DURATION,
    226.  
    227.             MediaStore.Audio.Media.DATA, // FOR PATH
    228.  
    229.             MediaStore.Audio.Media.ARTIST,
    230.  
    231. };
    232.  
    233.         Cursor cursor = context.getContentResolver().query(uri, projection, null,null,null);
    234.  
    235.         if (cursor!=null)
    236.  
    237.                 {
    238.  
    239.                 while(cursor.moveToNext())
    240.  
    241.                 {
    242.  
    243.                 String album = cursor.getString(0);
    244.  
    245.                 String title = cursor.getString(1);
    246.  
    247.                 String duration = cursor.getString(2);
    248.  
    249.                 String path = cursor.getString(3);
    250.  
    251.                 String artist = cursor.getString(4);
    252.  
    253.  
    254.                 MusicFiles musicFiles = new MusicFiles(path, title,artist,album,duration);
    255.  
    256.                 //take log.e for check
    257.  
    258.                 Log.e("Path: " + path, "Album : " + album);
    259.  
    260.                 tempAudioList.add(musicFiles)
    261.  
    262.                     }
    263.  
    264.                 cursor.close();
    265.  
    266.                  }
    267.  
    268.         return tempAudioList;
    269.  
    270.                 }
    271.  
    272.                 }

    [​IMG]

    [​IMG]

    AndroidManifest.xml

    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    3.     package="com.example.musicaudioplayer">
    4.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    5.     <application
    6.         android:allowBackup="true"
    7.         android:icon="[USER=22138]@Mipmap[/USER]/ic_launcher"
    8.         android:label="[USER=696546]@String[/USER]/app_name"
    9.         android:roundIcon="[USER=22138]@Mipmap[/USER]/ic_launcher_round"
    10.         android:supportsRtl="true"
    11.         android:theme="[USER=19691]@Style[/USER]/AppTheme">
    12.         <activity android:name=".MainActivity">
    13.             <intent-filter>
    14.                 <action android:name="android.intent.action.MAIN" />
    15.  
    16.                 <category android:name="android.intent.category.LAUNCHER" />
    17.             </intent-filter>
    18.         </activity>
    19.     </application>
    20.  
    21. </manifest>

    AlbumFragment.java

    Code (Java):
    1. package com.example.musicaudioplayer;
    2.  
    3. import android.os.Bundle;
    4.  
    5. import androidx.fragment.app.Fragment;
    6.  
    7. import android.view.LayoutInflater;
    8. import android.view.View;
    9. import android.view.ViewGroup;
    10.  
    11.  
    12. /**
    13. * A simple {@link Fragment} subclass.
    14. */
    15. public class AlbumFragment extends Fragment {
    16.  
    17.     public AlbumFragment() {
    18.         // Required empty public constructor
    19.     }
    20.  
    21.  
    22.     [USER=1021285]@override[/USER]
    23.     public View onCreateView(LayoutInflater inflater, ViewGroup container,
    24.                              Bundle savedInstanceState) {
    25.         // Inflate the layout for this fragment
    26.         View view = inflater.inflate(R.layout.fragment_album, container, false);
    27.         return view;
    28.     }
    29. }
    MusicFiles.java

    Code (Java):
    1. package com.example.musicaudioplayer;
    2.  
    3. public class MusicFiles {
    4.     private String path;
    5.     private String title;
    6.     private String artist;
    7.     private String album;
    8.     private String duration;
    9.  
    10.     public MusicFiles(String path, String title, String artist, String album, String duration) {
    11.         this.path = path;
    12.         this.title = title;
    13.         this.artist = artist;
    14.         this.album = album;
    15.         this.duration = duration;
    16.     }
    17.  
    18.     public MusicFiles() {
    19.     }
    20.  
    21.     public String getPath() {
    22.         return path;
    23.     }
    24.  
    25.     public void setPath(String path) {
    26.         this.path = path;
    27.     }
    28.  
    29.     public String getTitle() {
    30.         return title;
    31.     }
    32.  
    33.     public void setTitle(String title) {
    34.         this.title = title;
    35.     }
    36.  
    37.     public String getArtist() {
    38.         return artist;
    39.     }
    40.  
    41.     public void setArtist(String artist) {
    42.         this.artist = artist;
    43.     }
    44.  
    45.     public String getAlbum() {
    46.         return album;
    47.     }
    48.  
    49.     public void setAlbum(String album) {
    50.         this.album = album;
    51.     }
    52.  
    53.     public String getDuration() {
    54.         return duration;
    55.     }
    56.  
    57.     public void setDuration(String duration) {
    58.         this.duration = duration;
    59.     }
    60. }

    fragment_album.xml

    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3.     xmlns:tools="http://schemas.android.com/tools"
    4.     android:layout_width="match_parent"
    5.     android:layout_height="match_parent"
    6.     tools:context=".AlbumFragment">
    7.  
    8.     <!-- TODO: Update blank fragment layout -->
    9.     <TextView
    10.         android:layout_width="match_parent"
    11.         android:layout_height="match_parent"
    12.         android:text="[USER=696546]@String[/USER]/hello_blank_fragment" />
    13.  
    14. </FrameLayout>
    SongsFragment.java

    Code (Java):
    1. package com.example.musicaudioplayer;
    2.  
    3. import android.os.Bundle;
    4.  
    5. import androidx.fragment.app.Fragment;
    6.  
    7. import android.view.LayoutInflater;
    8. import android.view.View;
    9. import android.view.ViewGroup;
    10.  
    11.  
    12. /**
    13. * A simple {@link Fragment} subclass.
    14. */
    15. public class SongsFragment extends Fragment {
    16.  
    17.     public SongsFragment() {
    18.         // Required empty public constructor
    19.     }
    20.  
    21.  
    22.     [USER=1021285]@override[/USER]
    23.     public View onCreateView(LayoutInflater inflater, ViewGroup container,
    24.                              Bundle savedInstanceState) {
    25.         // Inflate the layout for this fragment
    26.         View view =inflater.inflate(R.layout.fragment_songs, container, false);
    27.         return view;
    28.     }
    29. }

    fragment_songs.xml



    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3.     xmlns:tools="http://schemas.android.com/tools"
    4.     android:layout_width="match_parent"
    5.     android:layout_height="match_parent"
    6.     tools:context=".SongsFragment">
    7.  
    8.     <!-- TODO: Update blank fragment layout -->
    9.     <TextView
    10.         android:layout_width="match_parent"
    11.         android:layout_height="match_parent"
    12.         android:text="[USER=696546]@String[/USER]/hello_blank_fragment" />
    13.  
    14. </FrameLayout>

    Ps.: I send also the ZIP file about the audio music player app, My Android version is below:

    [​IMG]




    Another trouble is the emulator that in my pc doesn´t work, i think it´s because my Pc doesn´t have the Intel processor but the AMD Processor. Could you help to fix this problem too? see the images

    [​IMG]


    [​IMG]


    Could anyone, help me, please to fix this problems?

    I expect your answer as soon as possible.

    Best regards

    José Moreira
     

    Attached Files:



    #1 Quencyjones79, Feb 28, 2021
    Last edited by a moderator: Feb 28, 2021

    1. Download the Forums for Android™ app!


      Download

       
  2. Unforgiven

    Unforgiven ...eschew obfuscation...
    Moderator

    I moved your thread to the development area for better views. :) I also added code tags for readability. Good luck!! :)

    Code Tag Usage:
    [code]Line 1
    Line 2
    Line 3
    Maintains white space
    Doesn't turn :) into emoji
    etc...
    [/code]
    Code (Text):
    1. Line 1
    2. Line 2
    3. Line 3
    4. Maintains white space
    5. Doesn't turn :) into emoji
    6. etc...
    7.  
     
Loading...

Share This Page

Loading...